package com.yonyou.iuap.controller.pc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yonyou.iuap.base.intf.IUserAdminService;
import com.yonyou.iuap.base.vo.UserVO;
import com.yonyou.iuap.pub.JsonUtils;
import com.yonyou.iuap.shell.entity.BdCategoryRelationEntity;
import com.yonyou.iuap.shell.entity.BdKnowledgeCategoryEntity;
import com.yonyou.iuap.shell.entity.exp.BdKnowledgeCategoryEntityVo;
import com.yonyou.iuap.shell.entity.exp.UserRolePoJo;
import com.yonyou.iuap.shell.pub.RequestDto;
import com.yonyou.iuap.shell.pub.ResultDto;
import com.yonyou.iuap.shell.service.impl.KmCategoryServiceImpl;
import com.yonyou.iuap.shell.service.intf.IKmCategoryService;
import com.yonyou.iuap.shell.service.intf.IKnowledgeCategoryService;

@Controller
@RequestMapping(value="/shellUser")
public class PCShellUserController {
	private final Logger logger = (Logger) LoggerFactory.getLogger(getClass());
	private long start,end;
	@Autowired
	private IUserAdminService iuserService;
	@Autowired
	private KmCategoryServiceImpl kmCategoryService;
	@Autowired
	private IUserAdminService userService;
	
	@RequestMapping(value="/queryAllUser")
	@ResponseBody
	public String queryAllUser(@RequestBody Map map,HttpServletRequest request,HttpServletResponse response){
		start=System.currentTimeMillis();
		ResultDto rsDto = new ResultDto();
		String user_name = map.get("user_name")==null?"":map.get("user_name").toString();
		String user_code = map.get("user_code")==null?"":map.get("user_code").toString();
		int pageSize = map.get("pageSize")==null?0:Integer.valueOf(map.get("pageSize").toString());
		int pageNum = map.get("pageNum")==null?0:Integer.valueOf(map.get("pageNum").toString());
		if (pageNum == 0)
			pageNum++;
		if (pageSize == 0)
			pageSize = 15;
		pageNum = (pageNum - 1) * pageSize;
		String whereSql = " 1=1";
		
		if(!StringUtils.isEmpty(user_name)){
			whereSql += " and (user_name like '%"+user_name+"%'";
			whereSql += " or  user_code like '%"+user_name+"%' )";
		}
		
		RequestDto reqDto = new RequestDto();
		reqDto.setParams(whereSql);
		reqDto.setPageNum(pageNum);
		reqDto.setPageSize(pageSize);
		rsDto = iuserService.selectByConditionPage(reqDto);
		end=System.currentTimeMillis();
		logger.info("queryAllUser接口调用时间"+String.valueOf(end-start));
		return JsonUtils.toJsonStr(rsDto);
	}
	@RequestMapping(value="/queryPkByUsercode")
	@ResponseBody
	public String queryPkByUsercode(@RequestBody Map map,HttpServletRequest request,HttpServletResponse response){
		start=System.currentTimeMillis();
		String user_name = map.get("user_name")==null?"":map.get("user_name").toString();
		String whereSql = " 1=1";
		whereSql += " and (pk_user = '"+user_name+"'";
		whereSql += " or  user_code = '"+user_name+"' )";
		ResultDto rsDto = new ResultDto();
		RequestDto reqDto = new RequestDto();
		reqDto.setParams(whereSql);
		reqDto.setPageNum(0);
		reqDto.setPageSize(1);
		rsDto = iuserService.selectByConditionPage(reqDto);
		if(rsDto==null || rsDto.getData()==null){
			return null;
		}else{
			List<UserVO> listvo = (List<UserVO>) rsDto.getData();
			rsDto.setData(listvo==null || listvo.size()==0?"":listvo.get(0).getPk_user());
			if(listvo==null || listvo.size()==0){
				rsDto.setStatus(1);
				rsDto.setMessage("用户不存在");
				return JsonUtils.toJsonStr(rsDto);
			}
			Map<String,String> param=new HashMap<String, String>();
			param.put("pk_user", listvo.get(0).getPk_user());
			UserRolePoJo user=queryUserRole(param);
			user.setPk_user(listvo.get(0).getPk_user());
			rsDto.setData(user);
			end=System.currentTimeMillis();
			logger.info("queryPkByUsercode接口调用时间"+String.valueOf(end-start));
			return JsonUtils.toJsonStr(rsDto);
		}
	}
	
	/**查询用户角色
	 * @param map pk_user 用户pk
	 * @return
	 */
	public UserRolePoJo queryUserRole(Map map){
		UserRolePoJo user=new UserRolePoJo();
		List<BdKnowledgeCategoryEntityVo> list= kmCategoryService.selectUserRole(map);
		ResultDto userDto = userService.checkUserIsSys(map.get("pk_user").toString());
		
		if(list!=null && list.size()>0)
		{
			List<String> pk_category=new ArrayList<String>();
			for(BdKnowledgeCategoryEntityVo bean:list){
				pk_category.add(bean.getPk_knowledge_category());
			}
			user.setPk_category(pk_category);
			if(userDto.getStatus()!=0){
				//类别管理员
				user.setUser_role(Role.MANAGER.toString());
			}else{
				//系统管理员及类别管理员
				user.setUser_role(Role.ADMINANDMANAGER.toString());
			}
		}else{
			if(userDto.getStatus()!=0){
				//普通用户
				user.setUser_role(Role.USER.toString());
			}else{
				//系统管理员
				user.setUser_role(Role.ADMIN.toString());
			}
		}
		return user;
	}
	
	public enum Role{
		ADMIN,MANAGER,USER,ADMINANDMANAGER
	}
}
